﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using RATSharp.Model;
using System.Collections.Generic;
using RATSharp.Rest;
using System.Threading.Tasks;
namespace RATSharpW7.ViewModel
{


    public class UserViewModel : SingleObjectViewModel<User, List<User>>
    {
        /// <summary>
        /// Stored static context of currently logged user
        /// </summary>
        public static User UserContext { get; set; }

        public void loadUser(string name, EventHandler<ModifyReservationEventArgs> eventHandler) 
        {
            Task<User> task = RestService.getInstance().getUserAsync(name);
            notifyChangeModel(task, eventHandler);
        }

       protected void notifyChangeModel(Task<User> task, EventHandler<ModifyReservationEventArgs> eventHandler)
        {
            task.ContinueWith(t =>
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    if (t.Exception != null || t.Result == null)
                    {
                        eventHandler(this, new ModifyReservationEventArgs(false, t.Exception != null ? t.Exception.Message : "unknown"));
                        UserContext = null;
                    }
                    else
                    {
                        UserContext = t.Result;
                        eventHandler(this, new ModifyReservationEventArgs(true, ""));
                    }
                });
            });
        }
    }
}
